<?php
/**
 * tpCommunity [a web Community based ThinkPHP5]
 *
 * @author yuan1994 <tianpian0805@gmail.com>
 * @link http://tpCommunity.yuan1994.com/
 * @copyright 2016 yuan1994 all rights reserved.
 * @license http://www.apache.org/licenses/LICENSE-2.0
 */

//------------------------
// 用户控制器
//-------------------------

namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\community\Controller;
use think\Db;
use think\Loader;

class CommunityUser extends Controller
{
    use \app\admin\traits\controller\Controller;

//    protected static $blacklist = ['delete', 'clear', 'deleteforever', 'recyclebin', 'recycle'];

    public function index()
    {
        $listRows=20;
        $asc=false;
        $order = $this->request->param('_order') ? : false;
        // 接受 sort参数 0 表示倒序 非0都 表示正序
        $sort = null !== $this->request->param('_sort')
            ? ($this->request->param('_sort') == 'asc' ? 'asc' : 'desc')
            : ($asc ? 'asc' : 'desc');
        $order_by = $order ? "b.{$order} {$sort}" : 'b.id desc';
        $map = ['a.isdelete'=>0,'b.is_admin'=>1];

        $list = Db::name('community')->alias('a')->field('b.id,a.lxr,a.lxr_dh,a.title,b.create_time,b.account,b.realname,b.last_login_time,b.email,b.mobile,b.login_count,a.status,b.remark')
            ->join([
                ['__COMMUNITY_USER__ b','b.ty_id=a.id','left'],
            ])
            ->where($map)
            ->order($order_by)
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        // 模板赋值显示
        $this->view->assign('list', $list);
        $this->view->assign("page", $list->render());
        $this->view->assign("count", $list->total());
        $this->view->assign('numPerPage', $listRows);

        return view('index',['list'=>$list, 'page'=>$list->render(), 'count'=>$list->total(), 'numPerPage'=>$listRows]);
    }

    protected function filter(&$map)
    {
        //不查询管理员
        $map['is_admin'] = 1;
        if ($this->request->param('realname')) {
            $map['realname'] = ["like", "%" . $this->request->param('realname') . "%"];
        }
        if ($this->request->param('account')) {
            $map['account'] = ["like", "%" . $this->request->param('account') . "%"];
        }
        if ($this->request->param('email')) {
            $map['email'] = ["like", "%" . $this->request->param('email') . "%"];
        }
        if ($this->request->param('mobile')) {
            $map['mobile'] = ["like", "%" . $this->request->param('mobile') . "%"];
        }
    }

    /**
     * 修改密码
     */
    public function password()
    {
        $id = $this->request->param('id/d');
        if ($this->request->isPost()) {
            //禁止修改管理员的密码，管理员id为1
            if ($id < 2) {
                return ajax_return_adv_error("缺少必要参数");
            }

            $password = $this->request->post('password');
            if (!$password) {
                return ajax_return_adv_error("密码不能为空");
            }
            if (false === Loader::model('CommunityUser')->updatePassword($id, $password)) {
                return ajax_return_adv_error("密码修改失败");
            }
            return ajax_return_adv("密码已修改为{$password}", '');
        } else {
            // 禁止修改管理员的密码，管理员 id 为 1

            return $this->view->fetch();
        }
    }

    /**
     * 禁用限制
     */
    protected function beforeForbid()
    {
        // 禁止禁用 Community 模块,权限设置节点
        $this->filterId(1, '该用户不能被禁用', '=');
    }
}